﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using SX.Utils.Castle.DynamicProxy;

namespace SX.Utils.Aop
{
    public class DaoInterceptor: IInterceptor
    {
        private Type _interfaceType;

        public DaoInterceptor(Type interfaceType)
        {
            this._interfaceType = interfaceType;
        }


        public void Intercept(IInvocation invocation)
        {
            var assembly = _interfaceType.Assembly;
            var file= assembly.GetManifestResourceStream(string.Format("{0}.Xml.Dao.{1}.xml", assembly.GetName().Name, _interfaceType.FullName));
            // 获取参数
            var parameters = invocation.Method.GetParameters();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Parse;

            // Create a reader over the input stream
            XmlReader xmlReader = XmlReader.Create(file, settings);
            // load the data into the document
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlReader);
           var element =  doc.GetElementById(invocation.Method.Name);
           var data = element.InnerText;
           invocation.ReturnValue = data.Trim();
        }
    }
}
